﻿@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IPageModelCatche<SteamGamesOverviewModel> _pageModelCatche
@inject IUserService _userService


<CnGalWebSite.Components.Containers.MiniModeContainer>
    @if (OverviewModel != null)
    {
        <div class="row">
            <div class="col-12 col-md-6">
                <HasMostGamesUsersCard Model="OverviewModel.HasMostGamesUsers" />
            </div>
            <div class="col-12 col-md-6">
                <PossessionRateHighestGamesCard Model="OverviewModel.PossessionRateHighestGames" />
            </div>
        </div>
        <AuthorizeView>
            <Authorized>
                @if (Model != null)
                {
                    <div id="game-library-card">
                        <CnGalWebSite.Shared.MasaComponent.PC.Users.UserPlayedGameListView IsCurrentUser="true" SteamId="@Model.SteamId" UserId="@Model.Id" IsAnniversary TotalCount="OverviewModel.Count" UserInfoModel="Model" />
                     </div>
                }
            </Authorized>
            <NotAuthorized>
                <MDCard Style="display: flex; justify-content: center;align-items: center;" Class="pa-3 mb-4">
                    <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="登入后分享游戏库" OnClick="_userService.Login" Icon="@IconType.Login.ToIconString()" />
                 </MDCard>
             </NotAuthorized>
         </AuthorizeView>
    }
    else
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill Class="mb-4" />
    }


    @if (isReady)
    {
        <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert Type="AlertTypes.Success" Class="mb-4" Color="tertiary">
            晒出的你的游戏库吧
        </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert>
        <CnGalWebSite.Shared.MasaComponent.PC.Comments.CommentTip CanComment CanDeleted="CanDeleted" ObjectId="2796" Type="CommentType.CommentArticle" Desc />
    }
</CnGalWebSite.Components.Containers.MiniModeContainer>


@code {

    public PersonalSpaceViewModel Model { get; set; }

    SteamGamesOverviewModel OverviewModel;

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    bool CanDeleted;
    bool isReady;

    protected override async Task OnInitializedAsync()
    {

        var user = await authenticationStateTask;
        if (user.User.Identity.IsAuthenticated)
        {
            try
            {
                string userId = userId = user.User.Claims.GetUserId();
                //获取用户基本信息
                Model = await _dataCacheService.PersonalSpaceDataCatche.GetCache(userId);
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取用户信息失败");
            }
        }

        if (user.User.IsInRole("Admin"))
        {
            CanDeleted = true;
        }
        isReady = true;

        try
        {
            OverviewModel = await _pageModelCatche.GetCache("api/steam/GetSteamGamesOverview");
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取所有Steam游戏数量失败");
        }
    }
}
